/*
 * SocketError.cpp
 *
 *  Created on: 2012-11-30
 *      Author: yangentao@gmail.com
 */

#include "SocketError.h"
#include <WinSock2.h>
#include <iostream>
#include "NetUtil.h"
#include "../StreamLog.h"

using namespace std;
namespace yet {

SocketError::SocketError() {
	errorCode = WSAGetLastError();
}
SocketError::SocketError(int errCode){
	errorCode = errCode;
}
SocketError::SocketError(const SocketError& other) {
	errorCode = other.errorCode;
}
SocketError::~SocketError() {
}

SocketError& SocketError::print() {
	slog << err <<"SocketError: "<< errorCode << "(" << getCodeName()<<"): "<<getMessage() << endln;
	return *this;
}

void SocketError::throwMe(){
	throw *this;
}
void SocketError::printThrow(){
	print();
	throw *this;
}
void SocketError::clearError() {
	WSASetLastError(0);
}

int SocketError::getCode() {
	return errorCode;
}
string SocketError::getMessage() {
	return getCodeName();//TODO localize
}
string SocketError::getCodeName() {
	return getSocketErrorCodeName(errorCode);
}

bool SocketError::isNetDown() {
	return errorCode == WSAENETDOWN;
}
bool SocketError::isNetReset() {
	return errorCode == WSAENETRESET;
}
bool SocketError::isConnReset() {
	return errorCode == WSAECONNRESET;
}
bool SocketError::isSocketShutdown() {
	return errorCode == WSAESHUTDOWN;
}
bool SocketError::isHostUnReach() {
	return errorCode == WSAEHOSTUNREACH;
}
bool SocketError::isAborted() {
	return errorCode == WSAECONNABORTED;
}
bool SocketError::isTimeout() {
	return errorCode == WSAETIMEDOUT;
}
bool SocketError::isWouldBlock(){
	return errorCode == WSAEWOULDBLOCK;
}

}
